home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1OMYLV (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  11.3 KB  |  413 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.event.DocumentEvent;
  4. import com.sun.java.swing.event.DocumentListener;
  5. import com.sun.java.swing.event.EventListenerList;
  6. import com.sun.java.swing.event.UndoableEditEvent;
  7. import com.sun.java.swing.event.UndoableEditListener;
  8. import com.sun.java.swing.event.DocumentEvent.EventType;
  9. import com.sun.java.swing.undo.CompoundEdit;
  10. import com.sun.java.swing.undo.UndoableEdit;
  11. import java.io.IOException;
  12. import java.io.ObjectInputStream;
  13. import java.io.PrintStream;
  14. import java.io.Serializable;
  15. import java.util.Dictionary;
  16. import java.util.Hashtable;
  17.  
  18. public abstract class AbstractDocument implements Document, Serializable {
  19.    private transient int numReaders;
  20.    private transient Thread currWriter;
  21.    private Dictionary documentProperties;
  22.    protected EventListenerList listenerList;
  23.    private Content data;
  24.    private AttributeContext context;
  25.    private static final String BAD_LOCK_STATE = "document lock failure";
  26.    protected static final String BAD_LOCATION = "document location failure";
  27.    public static final String ParagraphElementName = "paragraph";
  28.    public static final String ContentElementName = "content";
  29.    public static final String SectionElementName = "section";
  30.    public static final String ElementNameAttribute = "$ename";
  31.    static Class class$com$sun$java$swing$event$UndoableEditListener;
  32.    static Class class$com$sun$java$swing$event$DocumentListener;
  33.  
  34.    protected AbstractDocument(Content data) {
  35.       this(data, StyleContext.getDefaultStyleContext());
  36.    }
  37.  
  38.    protected AbstractDocument(Content data, AttributeContext context) {
  39.       this.documentProperties = null;
  40.       this.listenerList = new EventListenerList();
  41.       this.data = data;
  42.       this.context = context;
  43.    }
  44.  
  45.    public void addDocumentListener(DocumentListener listener) {
  46.       EventListenerList var10000 = this.listenerList;
  47.       Class var10001 = class$com$sun$java$swing$event$DocumentListener;
  48.       if (var10001 == null) {
  49.          try {
  50.             var10001 = Class.forName("com.sun.java.swing.event.DocumentListener");
  51.          } catch (ClassNotFoundException var2) {
  52.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  53.          }
  54.  
  55.          class$com$sun$java$swing$event$DocumentListener = var10001;
  56.       }
  57.  
  58.       var10000.add(var10001, listener);
  59.    }
  60.  
  61.    public void addUndoableEditListener(UndoableEditListener listener) {
  62.       EventListenerList var10000 = this.listenerList;
  63.       Class var10001 = class$com$sun$java$swing$event$UndoableEditListener;
  64.       if (var10001 == null) {
  65.          try {
  66.             var10001 = Class.forName("com.sun.java.swing.event.UndoableEditListener");
  67.          } catch (ClassNotFoundException var2) {
  68.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  69.          }
  70.  
  71.          class$com$sun$java$swing$event$UndoableEditListener = var10001;
  72.       }
  73.  
  74.       var10000.add(var10001, listener);
  75.    }
  76.  
  77.    protected Element createBranchElement(Element parent, AttributeSet a) {
  78.       return new BranchElement(this, parent, a);
  79.    }
  80.  
  81.    protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) {
  82.       return new LeafElement(this, parent, a, p0, p1);
  83.    }
  84.  
  85.    public synchronized Position createPosition(int offs) throws BadLocationException {
  86.       return this.data.createPosition(offs);
  87.    }
  88.  
  89.    public void dump(PrintStream out) {
  90.       Element root = this.getDefaultRootElement();
  91.       if (root instanceof AbstractElement) {
  92.          ((AbstractElement)root).dump(out, 0);
  93.       }
  94.  
  95.    }
  96.  
  97.    protected void fireChangedUpdate(DocumentEvent e) {
  98.       Object[] listeners = this.listenerList.getListenerList();
  99.  
  100.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  101.          Object var10000 = listeners[i];
  102.          Class var10001 = class$com$sun$java$swing$event$DocumentListener;
  103.          if (var10001 == null) {
  104.             try {
  105.                var10001 = Class.forName("com.sun.java.swing.event.DocumentListener");
  106.             } catch (ClassNotFoundException var4) {
  107.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  108.             }
  109.  
  110.             class$com$sun$java$swing$event$DocumentListener = var10001;
  111.          }
  112.  
  113.          if (var10000 == var10001) {
  114.             ((DocumentListener)listeners[i + 1]).changedUpdate(e);
  115.          }
  116.       }
  117.  
  118.    }
  119.  
  120.    protected void fireInsertUpdate(DocumentEvent e) {
  121.       Object[] listeners = this.listenerList.getListenerList();
  122.  
  123.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  124.          Object var10000 = listeners[i];
  125.          Class var10001 = class$com$sun$java$swing$event$DocumentListener;
  126.          if (var10001 == null) {
  127.             try {
  128.                var10001 = Class.forName("com.sun.java.swing.event.DocumentListener");
  129.             } catch (ClassNotFoundException var4) {
  130.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  131.             }
  132.  
  133.             class$com$sun$java$swing$event$DocumentListener = var10001;
  134.          }
  135.  
  136.          if (var10000 == var10001) {
  137.             ((DocumentListener)listeners[i + 1]).insertUpdate(e);
  138.          }
  139.       }
  140.  
  141.    }
  142.  
  143.    protected void fireRemoveUpdate(DocumentEvent e) {
  144.       Object[] listeners = this.listenerList.getListenerList();
  145.  
  146.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  147.          Object var10000 = listeners[i];
  148.          Class var10001 = class$com$sun$java$swing$event$DocumentListener;
  149.          if (var10001 == null) {
  150.             try {
  151.                var10001 = Class.forName("com.sun.java.swing.event.DocumentListener");
  152.             } catch (ClassNotFoundException var4) {
  153.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  154.             }
  155.  
  156.             class$com$sun$java$swing$event$DocumentListener = var10001;
  157.          }
  158.  
  159.          if (var10000 == var10001) {
  160.             ((DocumentListener)listeners[i + 1]).removeUpdate(e);
  161.          }
  162.       }
  163.  
  164.    }
  165.  
  166.    protected void fireUndoableEditUpdate(UndoableEditEvent e) {
  167.       Object[] listeners = this.listenerList.getListenerList();
  168.  
  169.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  170.          Object var10000 = listeners[i];
  171.          Class var10001 = class$com$sun$java$swing$event$UndoableEditListener;
  172.          if (var10001 == null) {
  173.             try {
  174.                var10001 = Class.forName("com.sun.java.swing.event.UndoableEditListener");
  175.             } catch (ClassNotFoundException var4) {
  176.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  177.             }
  178.  
  179.             class$com$sun$java$swing$event$UndoableEditListener = var10001;
  180.          }
  181.  
  182.          if (var10000 == var10001) {
  183.             ((UndoableEditListener)listeners[i + 1]).undoableEditHappened(e);
  184.          }
  185.       }
  186.  
  187.    }
  188.  
  189.    protected final AttributeContext getAttributeContext() {
  190.       return this.context;
  191.    }
  192.  
  193.    protected final Content getContent() {
  194.       return this.data;
  195.    }
  196.  
  197.    protected final synchronized Thread getCurrentWriter() {
  198.       return this.currWriter;
  199.    }
  200.  
  201.    public abstract Element getDefaultRootElement();
  202.  
  203.    public Dictionary getDocumentProperties() {
  204.       if (this.documentProperties == null) {
  205.          this.documentProperties = new Hashtable(2);
  206.       }
  207.  
  208.       return this.documentProperties;
  209.    }
  210.  
  211.    public final Position getEndPosition() {
  212.       Position p;
  213.       try {
  214.          p = this.createPosition(this.data.length());
  215.       } catch (BadLocationException var2) {
  216.          p = null;
  217.       }
  218.  
  219.       return p;
  220.    }
  221.  
  222.    public int getLength() {
  223.       return this.data.length() - 1;
  224.    }
  225.  
  226.    public final Object getProperty(Object key) {
  227.       return this.getDocumentProperties().get(key);
  228.    }
  229.  
  230.    public Element[] getRootElements() {
  231.       Element[] elems = new Element[1];
  232.       elems[0] = this.getDefaultRootElement();
  233.       return elems;
  234.    }
  235.  
  236.    public final Position getStartPosition() {
  237.       Position p;
  238.       try {
  239.          p = this.createPosition(0);
  240.       } catch (BadLocationException var2) {
  241.          p = null;
  242.       }
  243.  
  244.       return p;
  245.    }
  246.  
  247.    public String getText(int offset, int length) throws BadLocationException {
  248.       String str = this.data.getString(offset, length);
  249.       return str;
  250.    }
  251.  
  252.    public void getText(int offset, int length, Segment txt) throws BadLocationException {
  253.       this.data.getChars(offset, length, txt);
  254.    }
  255.  
  256.    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
  257.       if (str != null && str.length() != 0) {
  258.          try {
  259.             this.writeLock();
  260.             UndoableEdit u = this.data.insertString(offs, str);
  261.             DefaultDocumentEvent e = new DefaultDocumentEvent(this, offs, str.length(), EventType.INSERT);
  262.             if (u != null) {
  263.                e.addEdit(u);
  264.             }
  265.  
  266.             this.insertUpdate(e, a);
  267.             ((CompoundEdit)e).end();
  268.             this.fireInsertUpdate(e);
  269.             if (u != null) {
  270.                this.fireUndoableEditUpdate(new UndoableEditEvent(this, e));
  271.             }
  272.          } finally {
  273.             this.writeUnlock();
  274.          }
  275.  
  276.       }
  277.    }
  278.  
  279.    protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
  280.    }
  281.  
  282.    public final void putProperty(Object key, Object value) {
  283.       if (value != null) {
  284.          this.getDocumentProperties().put(key, value);
  285.       } else {
  286.          this.getDocumentProperties().remove(key);
  287.       }
  288.  
  289.    }
  290.  
  291.    protected final synchronized void readLock() {
  292.       try {
  293.          while(true) {
  294.             if (this.currWriter != null) {
  295.                this.wait();
  296.             } else {
  297.                ++this.numReaders;
  298.                break;
  299.             }
  300.          }
  301.       } catch (InterruptedException var1) {
  302.       }
  303.  
  304.    }
  305.  
  306.    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  307.       s.defaultReadObject();
  308.       this.listenerList = new EventListenerList();
  309.    }
  310.  
  311.    protected final synchronized void readUnlock() {
  312.       if (this.numReaders <= 0) {
  313.          throw new StateInvariantError("document lock failure");
  314.       } else {
  315.          --this.numReaders;
  316.          this.notify();
  317.       }
  318.    }
  319.  
  320.    public void remove(int offs, int len) throws BadLocationException {
  321.       if (len > 0) {
  322.          try {
  323.             this.writeLock();
  324.             DefaultDocumentEvent chng = new DefaultDocumentEvent(this, offs, len, EventType.REMOVE);
  325.             this.removeUpdate(chng);
  326.             UndoableEdit u = this.data.remove(offs, len);
  327.             if (u != null) {
  328.                chng.addEdit(u);
  329.             }
  330.  
  331.             ((CompoundEdit)chng).end();
  332.             this.fireRemoveUpdate(chng);
  333.             if (u != null) {
  334.                this.fireUndoableEditUpdate(new UndoableEditEvent(this, chng));
  335.             }
  336.          } finally {
  337.             this.writeUnlock();
  338.          }
  339.       }
  340.  
  341.    }
  342.  
  343.    public void removeDocumentListener(DocumentListener listener) {
  344.       EventListenerList var10000 = this.listenerList;
  345.       Class var10001 = class$com$sun$java$swing$event$DocumentListener;
  346.       if (var10001 == null) {
  347.          try {
  348.             var10001 = Class.forName("com.sun.java.swing.event.DocumentListener");
  349.          } catch (ClassNotFoundException var2) {
  350.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  351.          }
  352.  
  353.          class$com$sun$java$swing$event$DocumentListener = var10001;
  354.       }
  355.  
  356.       var10000.remove(var10001, listener);
  357.    }
  358.  
  359.    public void removeUndoableEditListener(UndoableEditListener listener) {
  360.       EventListenerList var10000 = this.listenerList;
  361.       Class var10001 = class$com$sun$java$swing$event$UndoableEditListener;
  362.       if (var10001 == null) {
  363.          try {
  364.             var10001 = Class.forName("com.sun.java.swing.event.UndoableEditListener");
  365.          } catch (ClassNotFoundException var2) {
  366.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  367.          }
  368.  
  369.          class$com$sun$java$swing$event$UndoableEditListener = var10001;
  370.       }
  371.  
  372.       var10000.remove(var10001, listener);
  373.    }
  374.  
  375.    protected void removeUpdate(DefaultDocumentEvent chng) {
  376.    }
  377.  
  378.    public void render(Runnable r) {
  379.       try {
  380.          this.readLock();
  381.          r.run();
  382.       } finally {
  383.          this.readUnlock();
  384.       }
  385.  
  386.    }
  387.  
  388.    public void setDocumentProperties(Dictionary x) {
  389.       this.documentProperties = x;
  390.    }
  391.  
  392.    protected final synchronized void writeLock() {
  393.       try {
  394.          while(this.numReaders > 0 || this.currWriter != null) {
  395.             this.wait();
  396.          }
  397.  
  398.          this.currWriter = Thread.currentThread();
  399.       } catch (InterruptedException var1) {
  400.       }
  401.  
  402.    }
  403.  
  404.    protected final synchronized void writeUnlock() {
  405.       if (this.numReaders <= 0 && this.currWriter != null) {
  406.          this.currWriter = null;
  407.          this.notify();
  408.       } else {
  409.          throw new StateInvariantError("document lock failure");
  410.       }
  411.    }
  412. }
  413.